package com.hemerasolutions.v2trainer.model.library;

import java.util.ArrayList;
import java.util.List;

import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.training.VersionableTraining;
import com.hemerasolutions.v2trainer.model.users.User;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.equipment.VersionableEquipment;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup;

@Entity
public class Library extends BaseModel {
 
	private static final long serialVersionUID = 1L;

    @Column(length = 30)
    private String name;
    
    @Column
    private boolean defaultLibrary;
    
    @Column(length = 1)
    private int shareType;
    
    @ManyToOne
	private User owner;

    @OneToMany(mappedBy="library", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
	private List<VersionableModality> versionableModalitys;
	 
    @OneToMany(mappedBy="library", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    private List<VersionableTraining> versionableTrainings; 

    @OneToMany(mappedBy="library", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
	private List<VersionableExerciseGroup> versionableExerciseGroups;

    @OneToMany(mappedBy="library", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
	private List<VersionableEquipment> versionableEquipments;
    
    public final static int SHARE_TYPE_PUBLIC = 1;

    public final static int SHARE_TYPE_AFFLIATES = 2;

    public final static int SHARE_TYPE_PERSONAL = 3;
    

    
	public Library() {
		super();
		this.versionableModalitys = new ArrayList<VersionableModality>();
		this.versionableTrainings = new ArrayList<VersionableTraining>();
		this.versionableExerciseGroups = new ArrayList<VersionableExerciseGroup>();
		this.versionableEquipments = new ArrayList<VersionableEquipment>();
		this.shareType = Library.SHARE_TYPE_PERSONAL;
	}
	
	public Library(User user) {
		this();
		this.owner = user;
		this.shareType = Library.SHARE_TYPE_PERSONAL;
	}

	public Library(Library other,User user){
		this();
		this.defaultLibrary = other.defaultLibrary;
		this.name = other.name;
		this.owner = user;
		this.shareType = other.shareType;

		for(VersionableModality aux :this.versionableModalitys){
			this.versionableModalitys.add(new VersionableModality(aux,this));
		}

		for(VersionableTraining aux :this.versionableTrainings){
			this.versionableTrainings.add(new VersionableTraining(aux,this));
		}
		
		for(VersionableExerciseGroup aux :this.versionableExerciseGroups){
			this.versionableExerciseGroups.add(new VersionableExerciseGroup(aux,this));
		}
				
		for(VersionableEquipment aux :this.versionableEquipments){
			this.versionableEquipments.add(new VersionableEquipment(aux,this));
		}
		
	}
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Integer getId() {
		return id;
	}
	
	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public boolean isDefaultLibrary() {
		return defaultLibrary;
	}

	public void setDefaultLibrary(boolean defaultLibrary) {
		this.defaultLibrary = defaultLibrary;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	public int getShareType() {
		return shareType;
	}

	public void setShareType(int shareType) {
		this.shareType = shareType;
	}


	public List<VersionableModality> getVersionableModalitys() {
		return versionableModalitys;
	}


	public void setVersionableModalitys(
			List<VersionableModality> versionableModalitys) {
		this.versionableModalitys = versionableModalitys;
	}


	public List<VersionableTraining> getVersionableTrainings() {
		return versionableTrainings;
	}


	public void setVersionableTrainings(
			List<VersionableTraining> versionableTrainings) {
		this.versionableTrainings = versionableTrainings;
	}


	public List<VersionableExerciseGroup> getVersionableExerciseGroups() {
		return versionableExerciseGroups;
	}


	public void setVersionableExerciseGroups(
			List<VersionableExerciseGroup> versionableExerciseGroups) {
		this.versionableExerciseGroups = versionableExerciseGroups;
	}


	public List<VersionableEquipment> getVersionableEquipments() {
		return versionableEquipments;
	}


	public void setVersionableEquipments(
			List<VersionableEquipment> versionableEquipments) {
		this.versionableEquipments = versionableEquipments;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((owner == null) ? 0 : owner.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Library other = (Library) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (owner == null) {
			if (other.owner != null)
				return false;
		} else if (!owner.equals(other.owner))
			return false;
		return true;
	}

	
	
	@Override
	public String toString() {
		return "Library [id=" + id + ", name=" + name + ", owner=" + owner
				+ "]";
	}
	 
}
 
